﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using HRProject.Code.Common;

namespace HRProject.User
{
    public partial class AddUserToRole : System.Web.UI.Page
    {
        private Guid userId;
        private List<string> roles;

        public List<string> AvailableRoles
        {
            get { return roles; }
            set { roles = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string userIdStr = Request.Params["userId"];
            try
            {
                userId = new Guid(userIdStr);
            }
            catch (Exception exp)
            {
                Console.Out.Write(exp.StackTrace);
                userId = Guid.Empty;
            }
            MembershipUser user = Membership.GetUser(userId);

            string[] allRoles = Roles.GetAllRoles();
            List<string> inRoles = new List<string>( Roles.GetRolesForUser(user.UserName));
            roles = new List<string>();
            foreach (string ar in allRoles)
            {
                if (inRoles.Contains(ar))
                {
                    inRoles.Remove(ar);
                }
                else
                {
                    roles.Add(ar);
                }
            }

            if (!IsPostBack)
            {
                lblUserName.Text = Request.Params["userName"];
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!BLAuthentication.IsAddAllowed)
            {
                lblError.Text = ContentProvider.GetMessage("NotAuthorized");
                return;
            }

            string[] addedRoles = Request.Params.GetValues("addedRole");
            Roles.AddUserToRoles(lblUserName.Text, addedRoles);

            Util.CloseDialog(Response);
        }
    }
}
